import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class DirectoryExample {

    public static void main(String[] args) {
        //demo();
        //demo2();
        demo3();
        //demo4();
    }

    private static void demo(){
        try(DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get("./nio/src"))){
            for(Path path : paths){
                System.out.println(path.getFileName());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void demo2(){
        try(DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get("./nio/src"),"*.java")){
            for(Path path : paths){
                System.out.println(path.getFileName());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void demo3(){
        String dirname = "./nio/src";
        DirectoryStream.Filter<Path> filter = (entry)->{
            if(Files.size(entry) > 25){
                return true;
            }
            return false;
        };
        try(DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get(dirname),filter)){
            for(Path path : paths){
                System.out.println(path.getFileName());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void demo4(){
        String dir = "./nio";
        try{
            Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>(){
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    System.out.println("正在访问文件："+file);
                    return super.visitFile(file, attrs);
                }

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    System.out.println("正在访问目录："+dir);
                    return super.preVisitDirectory(dir, attrs);
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    System.out.println("访问失败的文件："+file);
                    return super.visitFileFailed(file, exc);
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    System.out.println("这个目录访问结束了："+dir);
                    return super.postVisitDirectory(dir, exc);
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
